package com.bycx.dingxinlc.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.bycx.core.startup.AppConfig;
import com.bycx.core.util.HttpHelper;
import com.bycx.core.util.Md5Utils;
import com.bycx.core.util.StringUtils;
import com.bycx.dingxinlc.mapper.TCustLoginInfoMapper;
import com.bycx.dingxinlc.model.SmsValicode;
import com.bycx.dingxinlc.model.TCustLoginInfo;
import com.bycx.dingxinlc.service.WechatService;
import com.bycx.wechat.util.WeChatSessionUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class WechatServiceImpl implements WechatService {
	@Autowired
	private TCustLoginInfoMapper tCustLoginInfoMapper;
	@Autowired
	private TaskExecutor taskExecutor;

	@Override
	public boolean sendSmsValicode(String phoneNo) {
		Date now = new Date();

		// 发送验证码
		final Map<String, Object> msgMap = new HashMap<>();
		Map<String, Object> parameMap = new HashMap<>();
		Map<String, Object> msgMap3 = new HashMap<>();
		Map<String, Object> parameter = new HashMap<>();
		// 验证码
		String code = (Math.random() + "").substring(2, 8);
		String smsContent = AppConfig.get("sms_content").replace("code", code);
		parameter.put("message", AppConfig.get("sms_sign") + smsContent);
		msgMap3.put("parameter", parameter);
		msgMap3.put("phones", phoneNo);
		msgMap3.put("smsType", "27600002");
		msgMap3.put("templateNo", AppConfig.get("sms_template_no"));
		parameMap.put("31400001", msgMap3);
		msgMap.put("msgType", "31400001");
		msgMap.put("parameMap", parameMap);
		final String url = AppConfig.get("send_sms_url");
//		String res = HttpHelper.postJson(url, JSON.toJSONString(msgMap));
		// 异步方式防止阻塞
		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				HttpHelper.postJson(url, JSON.toJSONString(msgMap));
			}
		});

		SmsValicode smsValicode = new SmsValicode();
		smsValicode.setPhoneNo(phoneNo);
		smsValicode.setCode(code);
		smsValicode.setSendTime(now);
		smsValicode.setValidSec(AppConfig.getInteger("sms_valid_sec"));
		WeChatSessionUtil.setSmsValicode(smsValicode);

//		return JSONObject.parseObject(res).getBooleanValue("success");
		return true;
	}

	@Override
	public TCustLoginInfo doReg(String phone,
			String pwd,
			String openId) {

		String id = StringUtils.getUUID();
		String custPwd = Md5Utils.encode(Md5Utils.encode(pwd));
		Date now = new Date();

		TCustLoginInfo loginInfo = new TCustLoginInfo();
		loginInfo.setId(id);
		loginInfo.setPhoneNo(phone);
		loginInfo.setCustPwd(custPwd);
		loginInfo.setOpenId(openId);
		loginInfo.setCreateDate(now);
		tCustLoginInfoMapper.insertCustLoginInfo(loginInfo);

		return loginInfo;
	}

	@Override
	public TCustLoginInfo queryCustLoginInfoByPhoneNo(String phoneNo) {
		return tCustLoginInfoMapper.queryCustLoginInfoByPhoneNo(phoneNo);
	}

	@Override
	public TCustLoginInfo queryCustLoginInfoByOpenId(String openId) {
		return tCustLoginInfoMapper.queryCustLoginInfoByOpenId(openId);
	}

	@Override
	public void updateOpenIdById(String id,
			String openId) {
		tCustLoginInfoMapper.updateOpenIdById(id, openId, new Date());
	}

	@Override
	public void doFind(String phone,
			String newPwd) {
		tCustLoginInfoMapper.updatePwdByPhoneNo(phone, Md5Utils.encode(Md5Utils.encode(newPwd)), new Date());
	}

}
